<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Tests deriveBits() with various bad parameters for PBKDF2");

jsTestIsAsync = true;

var pbkdf2Key = null;

// Imports a PBKDF2 key and saves the result to pbkdf2Key.
function importPbkdf2Key() {
    var algorithm = {name: "PBKDF2"};

    var password = asciiToUint8Array("password");
    var usages = ["deriveBits", "deriveKey"];
    var extractable = false;

    // (1) Import the password
    return expectSuccess(
        "Importing the password...",
        crypto.subtle.importKey("raw", password, algorithm, extractable, usages).then(function(result) {
      pbkdf2Key = result;
    }));
}

importPbkdf2Key().then(function() {
    var algorithm = {
        name: "PBKDF2",
        salt: asciiToUint8Array("salt"),
        iterations: 1,
        hash: "SHA-1" 
    };

    return expectFailure(
        "Deriving 100 bits...",
        crypto.subtle.deriveBits(algorithm, pbkdf2Key, 100));
}).then(function() {
    var algorithm = {
        name: "PBKDF2",
        salt: asciiToUint8Array("salt"),
        iterations: 0,
        hash: "SHA-1" 
    };

    return expectFailure(
        "Deriving using iterations=0...",
        crypto.subtle.deriveBits(algorithm, pbkdf2Key, 16));
}).then(function() {
    var algorithm = {
        name: "PBKDF2",
        salt: asciiToUint8Array("salt"),
        iterations: -10,
        hash: "SHA-1" 
    };

    return expectFailure(
        "Deriving using iterations=-10...",
        crypto.subtle.deriveBits(algorithm, pbkdf2Key, 16));
}).then(function() {
    var algorithm = {
        name: "PBKDF2",
        salt: asciiToUint8Array("salt"),
        iterations: 1024,
        hash: "SHA-1" 
    };

    return expectFailure(
        "Deriving using length 0...",
        crypto.subtle.deriveBits(algorithm, pbkdf2Key, 0));
}).then(function() {
    var algorithm = {
        name: "PBKDF2",
        salt: asciiToUint8Array("salt"),
        iterations: 1024,
        hash: "SHA-1" 
    };

    return expectFailure(
        "Deriving using length -10...",
        crypto.subtle.deriveBits(algorithm, pbkdf2Key, -10));
}).then(finishJSTest, failAndFinishJSTest);

</script>

</body>
</html>
